package com.zndroid.utils.impl;

import android.text.TextUtils;

import java.text.DecimalFormat;
import java.util.regex.Pattern;

/**
 * @name:PatternUtil
 * @author:lazy
 * @email:luzhenyuxfcy@sina.com
 * @date : 2020/7/15 23:47
 * @version:
 * @description:正则相关
 */
@SuppressWarnings("unused")
public class PatternUtil {
    /**
     * base match use regular expression
     * */
    public boolean isMatch(String str, String regex) {
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(str).matches();
    }

    /**
     * check phoneNumber is valid
     * 宽泛方式
     *
     * @param phoneNumber phone number
     */
    public boolean isPhoneNumber(String phoneNumber) {
        //新号段在不断更新，不建议使用："^((13[0-9])|(15[^4,\\D])|(18[0,3,5-9]))\\d{8}$"
        String regex = "^1[0-9]{10}$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(phoneNumber).matches();
    }

    /**
     * check url is valid
     * @param url url
     * */
    public boolean isUrl(String url){
        String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(url).matches();
    }

    /**
     * check IP is valid
     * @param ip ip
     * */
    public boolean isIP(String ip){
        String regex = "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(ip).matches();
    }

    /**
     * check start with letter
     * @param str string
     * */
    public boolean isStartWithLetter(String str) {
        String regex = "^[A-Za-z][A-Za-z0-9]*$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(str).matches();
    }

    /**
     * check With Letter or Number
     * 含有英文或者数字
     * @param str string
     */
    public boolean isWithLetterOrNumber(String str) {
        String regex = "^[A-Za-z0-9]+$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(str).matches();
    }

    /**
     * 由数字、26个英文字母或者下划线组成的字符串
     * @param str string
     */
    public boolean isWithLetterAndNumberOr_(String str) {
        String regex = "^\\w+$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(str).matches();
    }

    /**
     * 中文、英文、数字包括下划线
     *
     * @param str string
     */
    public boolean isWithCNAndLetterAndNumberOr_(String str) {
        String regex = "^[\\u4E00-\\u9FA5A-Za-z0-9_]+$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(str).matches();
    }

    /**
     * 中文、英文、数字但不包括下划线等符号
     *
     * @param str string
     */
    public boolean isWithCNAndLetterAndNumberNot_(String str) {
        String regex = "^[\\u4E00-\\u9FA5A-Za-z0-9]+$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(str).matches();
    }

    /**
     * check is email format
     * @param email email
     * */
    public boolean isEmail(String email) {
        String regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(email).matches();
    }

    /**
     * check is car number format
     * @param carNumber car number
     * */
    public boolean isCar(String carNumber) {
        String regex = "^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(carNumber).matches();
    }

    /**
     * check is IDCard number format
     *
     * @param idCardNumber ID card number
     */
    public boolean isIDCard(String idCardNumber) {
        String regex = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(idCardNumber).matches();
    }

    /**
     * check is tel format
     * 验证固话
     * @param tel tel
     */
    public boolean isTel(String tel) {
        String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(tel).matches();
    }

    /**
     * 是否含有中文
     * @param str string
     * */
    public boolean isHasCN(String str) {
        String regex = "[\u4E00-\u9FA5|\\！|\\，|\\。|\\（|\\）|\\《|\\》|\\“|\\”|\\？|\\：|\\；|\\【|\\】]";
        Pattern pattern = Pattern.compile(regex.trim());
        return pattern.matcher(str).find();
    }

    /**
     * phoneNumber index of four to seven replace of '*'
     * 137****4561
     * @param phoneNumber phone number
     * */
    public String hidePhoneNumber(String phoneNumber) {
        String regex = "(\\d{3})\\d{4}(\\d{4})";
        return phoneNumber.replaceAll(regex, "$1 **** $2");
    }

    /**
     * 银行卡号，保留最后4位，其他星号替换
     *
     * @param cardId 卡号
     * @return 星号替换的银行卡号
     */
    public String hideBankCard(String cardId) {
        String regex = "\\d{15}(\\d{3})";
        return cardId.replaceAll(regex, "**** **** **** **** $1");
    }

    /**
     * 身份证号，中间10位星号替换
     *
     * @param idNumber 身份证号
     * @return 星号替换的身份证号
     */
    public String hideIDCard(String idNumber) {
        String regex = "(\\w{6})\\w*(\\w{3})";

        if (idNumber.length() == 15){
            idNumber = idNumber.replaceAll(regex, "$1 **** ** $2");
        } else if (idNumber.length() == 18){
            idNumber = idNumber.replaceAll(regex, "$1 **** **** *$2");
        }
        return idNumber;
    }

    /**
     * 格式化数字为千分位显示,如果是小数，保留二位有效数字
     * @param text 要格式化的数字
     * @return String
     */
    public String formatMicrometer(String text) {
        if (TextUtils.isEmpty(text)) {
            return "";
        }

        String dot = ".";
        DecimalFormat df;
        if (text.indexOf(dot) > 0) {
            int len = text.length() - text.indexOf(dot) - 1;
            if (len == 0) {
                df = new DecimalFormat("###,##0.");
            } else if (len == 1) {
                df = new DecimalFormat("###,##0.0");
            } else {
                df = new DecimalFormat("###,##0.00");
            }
        } else {
            df = new DecimalFormat("###,##0");
        }
        double number;
        try {
            number = Double.parseDouble(text);
        } catch (Exception e) {
            number = 0.0;
        }
        return df.format(number);
    }
}
